package com.sl.au.repository;

import com.sl.au.entity.News;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface NewsRepository extends JpaRepository<News, String> {
	
	//所有已发布与自己的信息
	@Query(" select m from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and (m.user.id = ?2 or m.isPublished=1)")
	Page<News> getAllNewsTitle(String key, String userId, Pageable pageable);

	@Query(" select count(*) from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and (m.user.id = ?2 or m.isPublished=1) ")
	Long getAllNewsCount(String key, String userId);
	
//	@Query(" select m from News m where m.user.saleArea.id = ?2 and (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') ")
//	Page<News> getNewsTitle(String key,String userId, Pageable pageable);
//
//	@Query(" select count(*) from News m where m.user.saleArea.id = ?2 and (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') ")
//	Long getNewsCount(String key,String userId);
	
	//本部门的发布信息（首页）
	@Query(" select m from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and m.isPublished = 1 and m.user.saleArea.superior = ?2 ")
	Page<News> getNewsTitle(String key, String superior, Pageable pageable);

	@Query(" select count(*) from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and m.isPublished = 1 and m.user.saleArea.superior = ?2")
	Long getNewsCount(String key, String superior);
	
	//本部门的发布信息（公司信息发布页面）
	@Query(" select m from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and ((m.isPublished = 1 and m.user.saleArea.superior = ?2) or m.user.saleArea.id = ?3) ")
	Page<News> getNewsTitle(String key, String superior,String userId,  Pageable pageable);

	@Query(" select count(*) from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and ((m.isPublished = 1 and m.user.saleArea.superior = ?2) or m.user.saleArea.id = ?3)")
	Long getNewsCount(String key, String superior,String userId);
	
	//本片区的发布信息
	@Query(" select m from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and ((m.isPublished = 1 and m.user.saleArea.id = ?2) or m.user.saleArea.id = ?3) ")
	Page<News> getAreaNewsTitle(String key, String areaId,String userId, Pageable pageable);

	@Query(" select count(*) from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and ((m.isPublished = 1 and m.user.saleArea.id = ?2) or m.user.saleArea.id = ?3) ")
	Long getAreaNewsCount(String key, String areaId,String uesrId);
	
	//自己的信息
	@Query(" select m from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and m.user.id = ?2")
	Page<News> getNewsUser(String key, String userId, Pageable pageable);

	@Query(" select count(*) from News m where (m.type like '%'+?1+'%' or m.title like '%'+?1+'%') and m.user.id = ?2 ")
	Long getNewsUserCount(String key, String userId);
}
